package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.CommentedPhotoRecordsDto;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.dto.PhotoRecordCommentDto;
import com.create404.suishoupai.entity.PhotoRecordComment;
import com.create404.suishoupai.service.IPhotoRecordCommentService;
import com.create404.suishoupai.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "照片记录评论接口")
@RestController
@RequestMapping("/photo-record-comment")
public class PhotoRecordCommentController {
    @Autowired
    private IPhotoRecordCommentService iPhotoRecordCommentService;

    @ApiOperation(value = "获取记录评论列表")
    @GetMapping("/getPhotoRecordCommentList")
    public Result getPhotoRecordCommentList(String photoRecordId){
        List<PhotoRecordCommentDto> photoRecordCommentDtoList = iPhotoRecordCommentService.getPhotoRecordCommentList(photoRecordId);
        return Result.ok(photoRecordCommentDtoList);
    }


    @PostMapping("/addPhotoRecordComment")
    public Result addPhotoRecordComment(String photoRecordId, String employeeId, String content) {
        PhotoRecordComment photoRecordComment = new PhotoRecordComment();
        photoRecordComment.setPhotoRecordId(photoRecordId);
        photoRecordComment.setEmployeeId(employeeId);
        photoRecordComment.setContent(content);
        photoRecordComment.setCreateTime(LocalDateTime.now());
        photoRecordComment.setUpdateTime(LocalDateTime.now());
        photoRecordComment.setTime(LocalDateTime.now());
        boolean result = iPhotoRecordCommentService.save(photoRecordComment);
        if (result) {
            return Result.ok("评论成功");
        } else {
            return Result.error("评论失败");
        }
    }

    @PostMapping("/getCommentedPhotoRecords")
    public Result getCommentedPhotoRecords(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<CommentedPhotoRecordsDto> commentedPhotoRecordsDtoPage = new Page<CommentedPhotoRecordsDto>(currentPage, pageSize);
        IPage<CommentedPhotoRecordsDto> page = iPhotoRecordCommentService.getCommentedPhotoRecords(commentedPhotoRecordsDtoPage, employeeId);
        return Result.ok(page);
    }
}
